package com.sk.sourcecircle.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sk.sourcecircle.R;
import com.sk.sourcecircle.callback.OnItemClickListener;
import com.sk.sourcecircle.entity.MyCity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by next on 2016/3/25.
 */
public class CityListAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<MyCity> allCities;

    private String[] firstLetterArray;// 存放存在的汉语拼音首字母
    private Map<String, Integer> letterIndex;
    private OnItemClickListener onItemClickListener;


    public CityListAdapter(Context context, List<MyCity> allCities, Map<String, Integer> letterIndex, OnItemClickListener itemClickListener) {
        this.context = context;
        this.allCities = allCities;
        this.letterIndex = letterIndex;
        this.onItemClickListener = itemClickListener;
        inflater = LayoutInflater.from(context);
        setup();
    }

    private void setup() {
        firstLetterArray = new String[allCities.size()];
        for (int i = 0; i < allCities.size(); i++) {
            // 当前汉语拼音首字母
            String currentStr = getAlpha(allCities.get(i).getPinyin());
            // 上一个汉语拼音首字母，如果不存在为" "
            String previewStr = (i - 1) >= 0 ? getAlpha(allCities.get(i - 1).getPinyin()) : " ";
            if (!previewStr.equals(currentStr)) {
                String name = getAlpha(allCities.get(i).getPinyin());
                letterIndex.put(name, i);
                firstLetterArray[i] = name;
            }
        }
    }

    @Override
    public int getCount() {
        return allCities.size();
    }

    @Override
    public Object getItem(int position) {
        return allCities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_city_list, parent, false);
            holder.letter = (TextView) convertView.findViewById(R.id.tv_sort);
            holder.name = (TextView) convertView.findViewById(R.id.city_name);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        if (position >= 0) {
            holder.name.setText(allCities.get(position).getCity_name());
            String currentStr = getAlpha(allCities.get(position).getPinyin());
            String previewStr = (position - 1) >= 0 ? getAlpha(allCities.get(position - 1).getPinyin()) : " ";
            if (!previewStr.equals(currentStr)) {
                holder.letter.setVisibility(View.VISIBLE);
                holder.letter.setText(currentStr);
            } else {
                holder.letter.setVisibility(View.GONE);
            }
        }

        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.clickItem(allCities.get(position));
                }
            }
        });
        return convertView;
    }

    public void updateListView(ArrayList<MyCity> fileterList) {

        if (fileterList == null) {
            this.allCities = new ArrayList<MyCity>();
        } else {
            this.allCities = fileterList;
        }
        notifyDataSetChanged();

    }

    class Holder {
        TextView letter, name;
    }


    // 获得汉语拼音首字母
    private String getAlpha(String str) {
        if (str == null) {
            return "#";
        }
        if (str.trim().length() == 0) {
            return "#";
        }
        char c = str.trim().substring(0, 1).charAt(0);
        // 正则表达式，判断首字母是否是英文字母
        Pattern pattern = Pattern.compile("^[A-Za-z]+$");
        if (pattern.matcher(c + "").matches()) {
            return (c + "").toUpperCase();
        } else if (str.equals("3")) {
            return "全部";
        } else {
            return "#";
        }
    }
}
